const esbuild = require("esbuild");
const path = require("path");
const fse = require("fs-extra");

esbuild
  .build({
    // 入口
    entryPoints: ["src/index.tsx"],
    // 启动sourcemap
    sourcemap: true,
    // 打包
    bundle: true,
    // 输出的目录
    outfile: "dist/index.js",
    /**
     * 配置额外的加载器
     */
    loader: { ".svg": "dataurl", ".ts": "tsx", ".js": "jsx" },
  })
  .then(async (res) => {
    const fileName = path.join(__dirname + "/dist/index.html");
    // 创建文件，如果文件不存在直接创建，存在不做任何事情
    await fse.ensureFile(fileName);
    // 把下面内容写入dist中的index.html文件中
    await fse.writeFileSync(
      fileName,
      `
 <!DOCTYPE html>
 <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试esbuild1</title>
    <link rel="stylesheet" href="./index.css">
  </head>
  <body>
    <div id="app">  APP1 </div>
  </body>
  <script type="module" src="./index.js"></script>
  <script>
    document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
    ':35729/livereload.js?snipver=1"></' + 'script>')
  </script>
  </script>
 </html>
 `
    );
  });
